define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'manufacture/quality/index' + location.search,
                    add_url: 'manufacture/quality/add',
                    edit_url: 'manufacture/quality/edit',
                    del_url: 'manufacture/quality/del',
                    multi_url: 'manufacture/quality/multi',
                    table: 'quality',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'quality_no', title: __('质检编号'), operate: 'LIKE'},
                        {field: 'standard.standard_name', title: __('质检标准'), operate: false},
                        {field: 'check_type', title: __('检查类型'), searchList: {"1":__('来料检验'),"2":__('过程检验'),"3":__('成品检验')}, formatter: function(value, row, index) {
                            var typeMap = {1: '来料检验', 2: '过程检验', 3: '成品检验'};
                            return typeMap[value] || '';
                        }},
                        {field: 'check_quantity', title: __('检查数量'), align: 'center'},
                        {field: 'pass_quantity', title: __('合格数量'), align: 'center'},
                        {field: 'fail_quantity', title: __('不合格数量'), align: 'center'},
                        {field: 'result', title: __('结果'), searchList: {"1":__('合格'),"0":__('不合格')}, formatter: function(value, row, index) {
                            if (value == 1) {
                                return '<span class="label label-success">合格</span>';
                            } else {
                                return '<span class="label label-danger">不合格</span>';
                            }
                        }},
                        {field: 'checker', title: __('检验员'), operate: 'LIKE'},
                        {field: 'check_time', title: __('检验时间'), formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('创建时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'operate', title: __('操作'), table: table, events: Table.api.events.operate, formatter: function(value, row, index) {
                            var table = this.table;
                            var buttons = [];
                            buttons.push({
                                name: 'detail',
                                text: __('详情'),
                                title: __('查看质检详情'),
                                classname: 'btn btn-xs btn-info btn-dialog',
                                icon: 'fa fa-eye',
                                url: 'manufacture/quality/detail/ids/{ids}',
                                callback: function (data) {
                                    table.bootstrapTable('refresh');
                                }
                            });
                            return Table.api.formatter.operate.call(this, value, row, index, buttons);
                        }}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
